package com.tmt.im.manager.service;

import com.alibaba.fastjson2.JSONObject;
import com.tmt.data.jpa.service.TmtJpaDataService;
import com.tmt.im.jpa.entity.SystemParam;
import com.tmt.im.jpa.repository.SystemParamRepo;
import com.tmt.im.jpa.systemparam.SystemParamInitiator;
import com.tmt.im.manager.config.beans.ClientInfoProperties;
import com.tmt.im.manager.controller.vo.VersionInfoVo;
import com.tmt.springboot.common.restful.ExecuteResult;
import com.tmt.springboot.common.restful.IExecuteResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.MessageFormat;

/**
 * @Description TODO
 * @Author wuyi
 * @Date 2024/9/20 16:09
 * @Version 1.0
 **/
@Service
@Slf4j
@Order(0)
public class SystemParamService extends TmtJpaDataService<SystemParam, Long> {

    private final SystemParamRepo systemParamRepo;

    private final ClientInfoProperties clientInfoProperties;

    public SystemParamService(SystemParamRepo mainRepo,
                              ClientInfoProperties clientInfoProperties) {
        super(mainRepo);
        this.systemParamRepo = mainRepo;
        this.clientInfoProperties = clientInfoProperties;
    }

    @Transactional(rollbackFor = Exception.class)
    public void InitializeTable() {
        try {
            log.info("----初始化系统参数----开始---");
            SystemParamInitiator.initial(clientInfoProperties, systemParamRepo);
            log.info("----初始化系统参数----结束---");
        } catch (Exception ex) {
            log.debug(ex.getMessage(), ex);
        }
    }

    public IExecuteResult<VersionInfoVo> getVersionInfo(String appType) {
        if (!"ios".equalsIgnoreCase(appType) && !"android".equalsIgnoreCase(appType)) {
            return ExecuteResult.failure(400, "无效的appType参数值,只能为ANDROID或IOS", null);
        }

        String paramCode = MessageFormat.format("{0}_version", appType.toLowerCase());
        SystemParam param = systemParamRepo.findOneByParamCode(paramCode);
        if (param == null) {
            return ExecuteResult.failure(MessageFormat.format("未找到{0}参数", paramCode), null);
        }

        VersionInfoVo vo = new VersionInfoVo();
        vo.setAppType(appType);
        vo.setName(param.getName());
        vo.setVersion(param.getParamValue());
        if (StringUtils.hasText(param.getParamExtend())) {
            try {
                JSONObject extend = JSONObject.parseObject(param.getParamExtend());
                vo.setUpgradeMsg(extend.getString("upgradeMsg"));
                vo.setForcedUpgrade(Boolean.TRUE.equals(extend.getBoolean("forcedUpgrade")));
                vo.setDownloadUrl(extend.getString("downloadUrl"));
                vo.setAbsolutePathUrl(extend.getString("absolutePathUrl"));
            } catch (Exception ex) {
                return ExecuteResult.failure("解析参数扩展信息出现异常：" + ex.getMessage(), null);
            }
        }
        return ExecuteResult.success(vo);
    }

}
